<!--
 * @Author: 刘十三
 * @Date: 2024-02-26 10:37:30
 * @LastEditors: 刘十三
 * @LastEditTime: 2024-04-10 10:56:20
 * @FilePath: \NM-dev-1.3.1\dev\pages\internal\linkage\todo\components\arrangeTaskQuestion.vue
 * @Description: 任务派发-核查问题
-->

<template>
  <view class="form-main">
    <u-form class="u-form-m" :model="question" :error-type="['border-bottom']" label-width="auto" label-position="top" ref="uForm">
      <u-form-item label="当天是否开门" :required="isEdit == 'edit'" :border-bottom="question.openDoorFlag == 1 ? true : false">
        <u-radio-group v-model="question.openDoorFlag" :disabled="isEdit == 'view'" @change="handleChange">
          <u-radio :name="1">是</u-radio>
          <u-radio :name="0">否</u-radio>
        </u-radio-group>
      </u-form-item>
      <template v-for="(item, index) in taskDistributedIssueVos">
        <u-form-item :label="item.tciName" :prop="item.tciId" :required="isEdit == 'edit'" :key="item.tciId" :border-bottom="index == taskDistributedIssueVos.length - 1 ? false : true" v-if="question.openDoorFlag == 1">
          <template v-if="item.tciType == 'radio'">
            <u-radio-group v-model="item.resultValue" :disabled="isEdit == 'view'">
              <u-radio name="1">是</u-radio>
              <u-radio name="0">否</u-radio>
            </u-radio-group>
          </template>
          <template v-if="item.tciType == 'text'">
            <u-input placeholder="请输入" v-model="item.resultValue" type="textarea" height="100" :auto-height="true" maxlength="500" :disabled="isEdit == 'view'" />
          </template>
        </u-form-item>
      </template>
    </u-form>
  </view>
</template>

<script>
export default {
  components: {},
  name: "arrangeTaskQuestion",
  props: {
    isEdit: {
      type: String,
      default: "edit",
    },
  },
  data() {
    return {
      question: {
        openDoorFlag: 1,
      },
      rules: {},
      taskDistributedIssueVos: [], // 要遍历的数据
    };
  },
  watch: {
    taskDistributedIssueVos: {
      deep: true,
      handler(list) {
        if (list.length > 0) {
          let form = {};
          let rulesFrom = {};
          list.forEach((item) => {
            Object.assign(form, { [`${item.tciId}`]: item.resultValue });
            Object.assign(rulesFrom, { [`${item.tciId}`]: [{ required: true, message: (item.tciType == "radio" ? "请选择" : "请输入") + item.tciName, trigger: ["blur", "change"] }] });
          });
          this.rules = rulesFrom;
          this.question = {
            ...form,
            openDoorFlag: this.question.openDoorFlag,
          };
          this.$refs.uForm.setRules(this.rules);
        }
      },
    },
  },
  methods: {
    handleChange(e) {
      console.log(e);
      if (e == 0) {
        this.$refs.uForm.setRules({});
      } else {
        this.$refs.uForm.setRules(this.rules);
      }
    },
    checkForm() {
      let self = this;

      return new Promise((resolve, reject) => {
        let form = Object.assign({}, self.question);
        let object = {
          taskDistributedIssueDtos: [],
          openDoorFlag: self.question.openDoorFlag,
        };
        if (self.question.openDoorFlag == 0) {
          resolve(object);
        } else {
          for (let key in form) {
            if (key != "openDoorFlag") {
              object.taskDistributedIssueDtos.push({
                tciId: key,
                resultValue: form[key],
              });
            }
          }
          self.$refs.uForm.validate((valid) => {
            if (valid) {
              resolve(object);
            } else {
              reject(2);
            }
          });
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.u-form-m {
  padding: 0px 5px;
}
</style>
